﻿using TTQT.TTQT_Code;
using TTQT.TTQT_Code.Info;

namespace TTQT.Administration
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ChangePassword.
	/// </summary>
	public partial class ChangePassword : MasterControl//System.Web.UI.UserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			this.lblUpdateStauts.Visible = false;
			if(Session["UserName"]==null)
			{
				Response.Redirect("Administration/Login.aspx");
			}
			else
			{
				this.txtUserName.Text = TTQTCommon.ToString(Session["UserName"]);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
        private void alert(string noiDung)
        {
            Page.ClientScript.RegisterClientScriptBlock(System.Type.EmptyTypes.GetType(), "focusElement", @"<script>
             alert('" + noiDung + @"');             
          </script>");
        }
		protected void cmdUpdate_Click(object sender, System.EventArgs e)
		{
			LogInfo log;
		    if(txtOldPassword.Text.Equals(txtNewPassword.Text))
		    {
		        alert("Mat khau moi trung voi mat khau cu.");
		        return;
		    }
			if(DataAccessObject.instance().changePassword(this.txtUserName.Text,TTQTCommon.MD5Encrypt(this.txtOldPassword.Text),TTQTCommon.MD5Encrypt(this.txtNewPassword.Text))>0)
			{
                DataAccessObject.instance().updateLastChangePass(userName);
				this.lblUpdateStauts.Text = "Doi mat khau thanh cong!";
				# region Ghi log
				try
				{
					log = new LogInfo();
					log.Ip = Request.UserHostAddress;
					log.Type = TTQTCommon.log_DoiMatKhau;
					log.UserName = userName;
					log.Content = "Đổi mật khẩu.";
					log.Brn_Code = UserInfo.Brn_Code;
					DataAccessObject.instance().addLog(log);
				}
				catch{}
				#endregion
			}
			else
			{
                alert("Sai mat khau cu.");
                return;
				# region Ghi log
				try
				{
					log = new LogInfo();
					log.Ip = Request.UserHostAddress;
					log.Type = TTQTCommon.log_DoiMatKhau;
					log.UserName = userName;
					log.Content = "Đổi mật khẩu không thành công.";
					//log.Brn_Code = this.brnCode;
					DataAccessObject.instance().addLog(log);
				}
				catch{}
				#endregion
			}
			this.lblUpdateStauts.Visible = true;
		}

		protected void cmdCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(navigateUrl(0));
		}
	}
}
